#include "ScaleItem.h"
#include "Map.h"
#include "MapRenderer.h"
#include <QPainter>


namespace TGS{
namespace MapControl{


ScaleItem::ScaleItem(Map* map)
	: _map(map)
{
	setZValue(0);
}


ScaleItem::~ScaleItem(void)
{
}

void ScaleItem::paint( QPainter* painter, const QStyleOptionGraphicsItem*, QWidget* )
{
	painter->save();

	const int PixLength = 50;
	const int painterHeight = painter->device()->height();
	const int y = painterHeight - 30;
	QPoint startPoint(20, y);
	QPoint endPoint(startPoint.x() + PixLength, y);

	const int topLength = 5;
	QPoint startTopPoint(startPoint.x(), startPoint.y() - topLength);
	QPoint endTopPoint(endPoint.x(), startPoint.y() - topLength);

	QPen pen(QColor(0, 0, 0));
	painter->drawLine(startPoint, endPoint);
	painter->drawLine(startPoint, startTopPoint);
	painter->drawLine(endPoint, endTopPoint);
	double length = _map->renderContext()->mapToPixel().mapUnitsPerPixel() * PixLength;
	QString strLength = QString::number(length, 'f', 2) + "m";
	QRect scaleRect(0, startPoint.y() - 3 * topLength, endPoint.x() + startPoint.x(), 3 * topLength);
	painter->drawText(scaleRect, Qt::AlignCenter, strLength);

	painter->restore();
}


}//MapControl
}//TGS